package com.frank.big;

import com.frank.big.util.GzipRequestInterceptor;
import com.frank.big.util.SHA1;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author 小石潭记
 * @date 2020/10/21 11:03
 * @Description: ${todo}
 */
@Slf4j
@Component
public class ApiClient {

    public static String callGet(String uid, String securityKey, String url, Boolean gzip) {
        String baseUrl = "https://testapi.big.com.cn/";
        String timestamp = System.currentTimeMillis() + "";
        String nonce = String.valueOf(new Random().nextInt());
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder().addInterceptor(new GzipRequestInterceptor());
        Request.Builder requestBuilder = new Request.Builder()
                .url(baseUrl + url)
                .get()
                .addHeader("Content-Type", "application/json")
                .addHeader("X-Uid", uid)
                .addHeader("X-Timestamp", timestamp)
                .addHeader("X-Nonce", nonce)
                .addHeader("X-Signature", SHA1.encrypt(nonce + ";" + securityKey + ";" + timestamp
                        + ";" + uid + ";"));
        if (gzip) {
            requestBuilder.addHeader("Accept-Encoding", "gzip");
        }
        Request request = requestBuilder.build();
        try {
            String result = okHttpClient.build().newCall(request).execute().body().string();
            log.info("查询的结果：{}", result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String callPost(String uid, String securityKey, String url, String json, Boolean gzip) {
        String baseUrl = "https://testapi.big.com.cn/";
        String nonce = String.valueOf(new Random().nextInt());
        String timestamp = System.currentTimeMillis() + "";
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder().addInterceptor(new GzipRequestInterceptor());
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, json);
        Request.Builder requestBuilder = new Request.Builder()
                .url(baseUrl + url)
                .post(body)
                .addHeader("Content-Type", "application/json")
                .addHeader("X-Uid", uid)
                .addHeader("X-Timestamp", timestamp)
                .addHeader("X-Nonce", nonce)
                .addHeader("X-Signature", SHA1.encrypt(nonce + ";" + securityKey + ";" + timestamp
                        + ";" + uid + ";"));
        if (gzip) {
            requestBuilder.addHeader("Accept-Encoding", "gzip");
        }
        Request request = requestBuilder.build();
        try {
            return okHttpClient.build().newCall(request).execute().body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println("----------开始调用GET方法----------");
        String result = callGet(
                "7W03RFHF",
                "T1YAEGbDOz7tIzq5cxff91jqG4PBtB75dY7IKcfqNS8ev0jYSJwtU4RSyqhQO8b9",
//                "enterprises?key=北京中数智汇科技股份有限公司&mask=10100110000&enableAggregate=false&page=0&size=20",
                "entinfo?id=010fff23665d13b277d31291ba56c5ff",
                false
        );
        log.info("查询的企业信息，{}", result);

        /*System.out.println("----------开始调用POST方法----------");
        String name = "南京衔鸣建设工程有限公司 北京五八信息技术有限公司";
        System.out.println(callPost(
                "7W03RFHF",
                "T1YAEGbDOz7tIzq5cxff91jqG4PBtB75dY7IKcfqNS8ev0jYSJwtU4RSyqhQO8b9",
                "enterprises?key=北京中数智汇科技股份有限公司&mask=10100110000&enableAggregate=false&page=0&size=20",
                "{\"entmark\": \"北京中数智汇科技股份有限公司\"}",
                true
        ));*/
    }

}
